package com.link.base.base.subjectchangeapply.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.user.model.User;
import com.link.base.base.accnt.dao.mybatis.mapper.AccountDetailMapper;
import com.link.base.base.accnt.dao.mybatis.mapper.AccountMapper;
import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.model.AccountDetail;
import com.link.base.base.accnt.model.BillInfo;
import com.link.base.base.accnt.service.AccountService;
import com.link.base.base.approval.service.FlowService;
import com.link.base.base.position.dao.mybatis.mapper.PositionMapper;
import com.link.base.base.subjectchangeapply.dao.mybatis.mapper.SubjectChangeApplyMapper;
import com.link.base.base.subjectchangeapply.model.SubjectChangeApply;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/***
 * @author liushoulong
 * @date 2018-11-08
 * @desc 信息变更申请
 **/
@Service
public class SubjectChangeApplyServiceImpl extends BasicServiceImpl<SubjectChangeApply> implements SubjectChangeApplyService {

    @Resource
    private SubjectChangeApplyMapper subjectChangeApplyMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private AccountDetailMapper accountDetailMapper;
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private FlowService flowService;

    @Override
    public BasicMapper<SubjectChangeApply> getBasicMapper() {
        return subjectChangeApplyMapper;
    }

    /**
     * 新增
     *
     * @author 赵换玲
     */
    @Override
    public void insert(SubjectChangeApply entity) throws Exception {
        CoreUser user = UserUtil.getUser();
        Account account1 = new Account();
        Account account2 = new Account();
        account1.setId(entity.getAccntId());
        account2 = accountService.queryById(account1);
        entity.setApplicantId(user.getId());
        entity.setApplicant(user.getFirstName());
        entity.setPrLegalContName(account2.getLegalContName());
        entity.setUserId(user.getId());
        BillInfo billInfo = new BillInfo();
        //添加社会信用代码
        if (account2.getMainBillAccntId() != null) {
            billInfo.setId(account2.getMainBillAccntId());
            /*BillInfo billInfo1 = billInfoMapper.queryById(billInfo);
            // 获取社会信用代码
            entity.setPrCreditNo(billInfo1.getTaxpayerNumber());*/
        }

        super.insert(entity);
    }

    /**
     * 查询指定用户信息表更列表
     * @return List
     * @throws Exception
     */
    @Override
    public List<SubjectChangeApply> queryByUserExamplePage(User user) throws Exception {
        List<SubjectChangeApply> subjectChangeApplyList = subjectChangeApplyMapper.queryByUserExamplePage(user);
        return subjectChangeApplyList;
    }

    /**
     * 提交更改信息审批
     * author 赵换玲
     *
     * @param subjectChangeApply
     * @param approvalDetail
     * @throws Exception
     */
    @Override
    public void accntSigningSubmit(SubjectChangeApply subjectChangeApply, String approvalDetail) throws Exception {
        Long selectUserId = null;
        String approvalType = "ModifyInformExpense";
        //创建审批流程
        flowService.createFlow(subjectChangeApply.getId(),
                approvalType, selectUserId, subjectChangeApply.getPrAccuntName(), subjectChangeApply.getAccntId(), approvalDetail);
        subjectChangeApply.setApplyStatus("auditing");
        subjectChangeApplyMapper.update(subjectChangeApply);
    }

    /**
     * 审批通过后更新客户信息及申请信息状态
     */
    @Override
    public void subjectStatusUpdate(Long objectId, String param1, String param2, String param3, String param4, String param5) throws Exception {
        /*申请信息*/
        CoreUser user = UserUtil.getUser();
        SubjectChangeApply queryObject = new SubjectChangeApply();
        queryObject.setId(objectId);
        SubjectChangeApply entity = queryById(queryObject);
        entity.setPrincipal(user.getFirstName());
        entity.setPrincipald(user.getId());
        //审批拒绝
        if ("Refuse".equals(param1)) {
            //已拒绝
            entity.setApplyStatus(param2);
            subjectChangeApplyMapper.update(entity);
        }
        //审批通过
        if ("Approved".equals(param1)) {
            Account account = new Account();
            account.setId(entity.getAccntId());
            Account account1 = accountMapper.queryById(account);
            //法人
            if (!StringUtils.isBlank(entity.getLegalContName())) {
                account1.setLegalContName(entity.getLegalContName());

            }
            //社会信用代码
            if (!StringUtils.isBlank(entity.getCreditNo())) {
                account1.setCreditNo(entity.getCreditNo());
                if (account1.getMainBillAccntId() != null) {
                    BillInfo billInfo = new BillInfo();
                    billInfo.setId(account1.getMainBillAccntId());
                    //更新社会信用代码
                    /*BillInfo billInfo1 = billInfoMapper.queryById(billInfo);
                    billInfo1.setTaxpayerNumber(entity.getCreditNo());
                    billInfoMapper.update(billInfo1);*/
                }
            }
            //客户名称
            if (!StringUtils.isBlank(entity.getAccuntName())) {
                account1.setAcctName(entity.getAccuntName());
            }
            AccountDetail accountDetail = new AccountDetail();
            accountDetail.setId(account1.getId());
            //三证合一
            if (!StringUtils.isBlank(entity.getTripleCerUrl()) && !StringUtils.isBlank(entity.getTripleCerBigUrl())) {
                accountDetail.setThreeInOneUrl(entity.getTripleCerUrl());
                accountDetail.setThreeInOneBigUrl(entity.getTripleCerBigUrl());
            } //三证合一副本
            if (!StringUtils.isBlank(entity.getTripleCerCopyUrl()) && !StringUtils.isBlank(entity.getTripleCerCopyBigUrl())) {
                accountDetail.setCorporateImagePhotoUrl(entity.getTripleCerCopyUrl());
                accountDetail.setCorporateImagePhotoBigUrl(entity.getTripleCerCopyBigUrl());
            }
            //一般纳税人证明
            if (!StringUtils.isBlank(entity.getTexpayerCerUrl()) && !StringUtils.isBlank(entity.getTexpayerCerBigUrl())) {
                accountDetail.setTaxPayerUrl(entity.getTexpayerCerUrl());
                accountDetail.setTaxPayerBigUrl(entity.getTexpayerCerBigUrl());
            }
            //开户许可证
            if (!StringUtils.isBlank(entity.getOpenPermitUrl()) && !StringUtils.isBlank(entity.getOpenPermitBigUrl())) {
                accountDetail.setLicenceForAccountUrl(entity.getOpenPermitUrl());
                accountDetail.setLicenceForAccountBigUrl(entity.getOpenPermitBigUrl());
            }
            //身份证正面
            if (!StringUtils.isBlank(entity.getIdCardUrl()) && !StringUtils.isBlank(entity.getIdCardBigUrl())) {
                accountDetail.setIdCardFrontUrl(entity.getIdCardUrl());
                accountDetail.setIdCardFrontBigUrl(entity.getIdCardBigUrl());
            }
            // 身份证反面
            if (!StringUtils.isBlank(entity.getReverseIdCardUrl()) && !StringUtils.isBlank(entity.getReverseIdCardBigUrl())) {
                accountDetail.setIdCardReverseUrl(entity.getReverseIdCardUrl());
                accountDetail.setIdCardReverseBigUrl(entity.getReverseIdCardBigUrl());
            }

            entity.setApplyStatus(param2);
            accountMapper.update(account1);
            accountDetailMapper.update(accountDetail);
            subjectChangeApplyMapper.update(entity);
        }

    }

    @Override
    public SubjectChangeApply queryById(SubjectChangeApply entity) throws Exception {
        SubjectChangeApply subjectChangeApply = subjectChangeApplyMapper.queryById(entity);
        return subjectChangeApply;
    }


    /**
     * 添加要更改的企业认证图片
     *
     * @param entity
     * @throws Exception
     */
    @Override
    public void updateCertificateImg(SubjectChangeApply entity) throws Exception {

        SubjectChangeApply params = new SubjectChangeApply();
        params.setId(entity.getId());
        //三证合一
        if (!StringUtils.isBlank(entity.getTripleCerUrl()) && !StringUtils.isBlank(entity.getTripleCerBigUrl())) {
            params.setTripleCerUrl(entity.getTripleCerUrl());
            params.setTripleCerBigUrl(entity.getTripleCerBigUrl());
        }
        //三证合一副本
        if (!StringUtils.isBlank(entity.getTripleCerCopyUrl()) && !StringUtils.isBlank(entity.getTripleCerCopyBigUrl())) {
            params.setTripleCerCopyUrl(entity.getTripleCerCopyUrl());
            params.setTripleCerCopyBigUrl(entity.getTripleCerCopyBigUrl());
        }
        //一般纳税人证明
        if (!StringUtils.isBlank(entity.getTexpayerCerUrl()) && !StringUtils.isBlank(entity.getTexpayerCerBigUrl())) {
            params.setTexpayerCerUrl(entity.getTexpayerCerUrl());
            params.setTexpayerCerBigUrl(entity.getTexpayerCerBigUrl());
        }
        //开户许可证
        if (!StringUtils.isBlank(entity.getOpenPermitUrl()) && !StringUtils.isBlank(entity.getOpenPermitBigUrl())) {
            params.setOpenPermitUrl(entity.getOpenPermitUrl());
            params.setOpenPermitBigUrl(entity.getOpenPermitBigUrl());
        }
        //身份证正面
        if (!StringUtils.isBlank(entity.getIdCardUrl()) && !StringUtils.isBlank(entity.getIdCardBigUrl())) {
            params.setIdCardUrl(entity.getIdCardUrl());
            params.setIdCardBigUrl(entity.getIdCardBigUrl());
        }
        // 身份证反面
        if (!StringUtils.isBlank(entity.getReverseIdCardUrl()) && !StringUtils.isBlank(entity.getReverseIdCardBigUrl())) {
            params.setReverseIdCardUrl(entity.getReverseIdCardUrl());
            params.setReverseIdCardBigUrl(entity.getReverseIdCardBigUrl());
        }
        subjectChangeApplyMapper.updateCertificateImg(params);

    }
}
